{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "naval-assist",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "\n",
    "DATA_PATH = r'./data/'\n",
    "PROCESSED_DATA_PATH = r'./processed_data/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "monetary-brisbane",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>loanAmnt</th>\n",
       "      <th>term</th>\n",
       "      <th>interestRate</th>\n",
       "      <th>installment</th>\n",
       "      <th>grade</th>\n",
       "      <th>subGrade</th>\n",
       "      <th>employmentTitle</th>\n",
       "      <th>employmentLength</th>\n",
       "      <th>homeOwnership</th>\n",
       "      <th>...</th>\n",
       "      <th>n12</th>\n",
       "      <th>n13</th>\n",
       "      <th>n14</th>\n",
       "      <th>load_amt_level</th>\n",
       "      <th>interest_level</th>\n",
       "      <th>income_level</th>\n",
       "      <th>dti_level</th>\n",
       "      <th>delinquency_2years_level</th>\n",
       "      <th>loan_amt_level</th>\n",
       "      <th>total_acc_level</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>35000.0</td>\n",
       "      <td>5</td>\n",
       "      <td>19.52</td>\n",
       "      <td>917.97</td>\n",
       "      <td>E</td>\n",
       "      <td>E2</td>\n",
       "      <td>320.0</td>\n",
       "      <td>2 years</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>18000.0</td>\n",
       "      <td>5</td>\n",
       "      <td>18.49</td>\n",
       "      <td>461.90</td>\n",
       "      <td>D</td>\n",
       "      <td>D2</td>\n",
       "      <td>219843.0</td>\n",
       "      <td>5 years</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>12000.0</td>\n",
       "      <td>5</td>\n",
       "      <td>16.99</td>\n",
       "      <td>298.17</td>\n",
       "      <td>D</td>\n",
       "      <td>D3</td>\n",
       "      <td>31698.0</td>\n",
       "      <td>8 years</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>11000.0</td>\n",
       "      <td>3</td>\n",
       "      <td>7.26</td>\n",
       "      <td>340.96</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>46854.0</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>3000.0</td>\n",
       "      <td>3</td>\n",
       "      <td>12.99</td>\n",
       "      <td>101.07</td>\n",
       "      <td>C</td>\n",
       "      <td>C2</td>\n",
       "      <td>54.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 54 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  loanAmnt  term  interestRate  installment grade subGrade  \\\n",
       "0   0   35000.0     5         19.52       917.97     E       E2   \n",
       "1   1   18000.0     5         18.49       461.90     D       D2   \n",
       "2   2   12000.0     5         16.99       298.17     D       D3   \n",
       "3   3   11000.0     3          7.26       340.96     A       A4   \n",
       "4   4    3000.0     3         12.99       101.07     C       C2   \n",
       "\n",
       "   employmentTitle employmentLength  homeOwnership  ...  n12  n13  n14  \\\n",
       "0            320.0          2 years              2  ...  0.0  0.0  2.0   \n",
       "1         219843.0          5 years              0  ...  NaN  NaN  NaN   \n",
       "2          31698.0          8 years              0  ...  0.0  0.0  4.0   \n",
       "3          46854.0        10+ years              1  ...  0.0  0.0  1.0   \n",
       "4             54.0              NaN              1  ...  0.0  0.0  4.0   \n",
       "\n",
       "   load_amt_level  interest_level  income_level  dti_level  \\\n",
       "0               7               7             6          7   \n",
       "1               5               7             3          8   \n",
       "2               4               6             4          8   \n",
       "3               4               1             6          7   \n",
       "4               2               4             3          9   \n",
       "\n",
       "   delinquency_2years_level  loan_amt_level  total_acc_level  \n",
       "0                         0               7                6  \n",
       "1                         0               5                5  \n",
       "2                         0               4                6  \n",
       "3                         0               4                6  \n",
       "4                         0               2                6  \n",
       "\n",
       "[5 rows x 54 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_pickle(PROCESSED_DATA_PATH + \"data_add.pkl\")\n",
    "data.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "communist-mercury",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(800000, 9)\n",
      "(800000, 78)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(800000, 93)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dummy_fields = ['loan_amt_level','interest_level','income_level','dti_level', 'delinquency_2years_level','grade', 'homeOwnership','total_acc_level','employmentLength']\n",
    "print(data[dummy_fields].shape)\n",
    "df_feature = pd.get_dummies(data[dummy_fields])\n",
    "print(df_feature.shape)\n",
    "\n",
    "behavior_fields = [f\"n{i}\" for i in range(15)]\n",
    "min_max = MinMaxScaler()\n",
    "df_behavior = pd.DataFrame(min_max.fit_transform(data[behavior_fields]),columns=behavior_fields)\n",
    "df_behavior.shape\n",
    "\n",
    "df_to_train = pd.concat([df_feature, df_behavior],axis=1)\n",
    "df_to_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "convenient-consent",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['homeOwnership', 'loan_amt_level_0', 'loan_amt_level_1',\n",
       "       'loan_amt_level_2', 'loan_amt_level_3', 'loan_amt_level_4',\n",
       "       'loan_amt_level_5', 'loan_amt_level_6', 'loan_amt_level_7',\n",
       "       'interest_level_0', 'interest_level_1', 'interest_level_2',\n",
       "       'interest_level_3', 'interest_level_4', 'interest_level_5',\n",
       "       'interest_level_6', 'interest_level_7', 'interest_level_8',\n",
       "       'interest_level_9', 'income_level_0', 'income_level_1',\n",
       "       'income_level_2', 'income_level_3', 'income_level_4', 'income_level_5',\n",
       "       'income_level_6', 'income_level_7', 'income_level_8', 'income_level_9',\n",
       "       'income_level_10', 'dti_level_0', 'dti_level_1', 'dti_level_2',\n",
       "       'dti_level_3', 'dti_level_4', 'dti_level_5', 'dti_level_6',\n",
       "       'dti_level_7', 'dti_level_8', 'dti_level_9', 'dti_level_10',\n",
       "       'dti_level_11', 'delinquency_2years_level_0',\n",
       "       'delinquency_2years_level_1', 'delinquency_2years_level_2',\n",
       "       'delinquency_2years_level_3', 'delinquency_2years_level_4',\n",
       "       'delinquency_2years_level_5', 'delinquency_2years_level_6',\n",
       "       'delinquency_2years_level_7', 'grade_A', 'grade_B', 'grade_C',\n",
       "       'grade_D', 'grade_E', 'grade_F', 'grade_G', 'total_acc_level_0',\n",
       "       'total_acc_level_1', 'total_acc_level_2', 'total_acc_level_3',\n",
       "       'total_acc_level_4', 'total_acc_level_5', 'total_acc_level_6',\n",
       "       'total_acc_level_7', 'total_acc_level_8', 'total_acc_level_9',\n",
       "       'employmentLength_1 year', 'employmentLength_10+ years',\n",
       "       'employmentLength_2 years', 'employmentLength_3 years',\n",
       "       'employmentLength_4 years', 'employmentLength_5 years',\n",
       "       'employmentLength_6 years', 'employmentLength_7 years',\n",
       "       'employmentLength_8 years', 'employmentLength_9 years',\n",
       "       'employmentLength_< 1 year', 'n0', 'n1', 'n2', 'n3', 'n4', 'n5', 'n6',\n",
       "       'n7', 'n8', 'n9', 'n10', 'n11', 'n12', 'n13', 'n14'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_to_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "robust-nation",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df_to_train.fillna(0).values, data['isDefault'].values, test_size=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "separated-preservation",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/shis/.conda/envs/nlptorch/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py:765: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "LogisticRegression()"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lr = LogisticRegression()\n",
    "lr.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "supreme-wheat",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      0.99      0.89     64141\n",
      "           1       0.52      0.04      0.08     15859\n",
      "\n",
      "    accuracy                           0.80     80000\n",
      "   macro avg       0.66      0.52      0.48     80000\n",
      "weighted avg       0.75      0.80      0.73     80000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "y_pred = lr.predict(X_test)\n",
    "\n",
    "r = classification_report(y_test, y_pred)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "narrative-terrorist",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.81      0.98      0.89     64141\n",
      "           1       0.47      0.06      0.10     15859\n",
      "\n",
      "    accuracy                           0.80     80000\n",
      "   macro avg       0.64      0.52      0.50     80000\n",
      "weighted avg       0.74      0.80      0.73     80000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "rf = RandomForestClassifier()\n",
    "\n",
    "rf.fit(X_train, y_train)\n",
    "y_pred2 = rf.predict(X_test)\n",
    "\n",
    "r2 = classification_report(y_test, y_pred2)\n",
    "print(r2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
